from flask import Blueprint,request,jsonify
from utils.comm import get_data
from db import db1
bp_course = Blueprint('course',__name__)

#无限级分类重构
def xtree(data):
    #先判断data没有直接返回
    if not data:
        return data
    #遍历data变成字典
    tree={}
    list=[]
    for i in data:
        tree[i['id']] = i

    for j in data:
        #获取父id
        pid = j['pid']
        if pid == 0:
            #说明顶级分类直接加入列表
            list.append(j)
        else:
            #查看父类下有没有子类
            if 'son' not in tree[pid]:
                #如果没有初始化列表
                tree[pid]['son'] = []
            #把子类加到父类列表中
            tree[pid]['son'].append(j)
    return list

@bp_course.route('/cateM',methods=['POST','GET'])
def cateM():
    if request.method == 'POST':
        #添加
        #获取数据
        data = get_data()
        print(data)
        #构造sql
        sql = "insert into cates values(0,'%s',%d,%s)"%(data['name'],int(data['pid']),data['params'])
        print(sql)
        #调用db1
        db1.update(sql)
        #返回结果
        return jsonify({"code":200})
    else:
        #无限级分类展示
        sql = "select * from cates"
        res = db1.find_all(sql)
        data = xtree(res)
        return jsonify({"code":200,'list':data})




@bp_course.route('/pagecates')
def pagecates():
    sql = "select * from cates order by id asc"
    # sql = "select c.name,c.params,t.name as tname from cates as c left join cates as t on c.pid=t.id order by c.id desc"
    res = db1.find_all(sql)
    return jsonify({"code":200,'list':res})




@bp_course.route('/pagecates')
def pagecates():
    #每页面显示多少条
    page_size=2
    #当前页
    page = request.args.get('page')
    start = (int(page)-1)*page_size
    sql = "select * from cates  order by id asc limit %d,%d"%(start,page_size)
    # sql = "select c.name,c.params,t.name as tname from cates as c left join cates as t on c.pid=t.id order by c.id desc"
    res = db1.find_all(sql)
    #查询共有多少条记录
    sql1 = "select count(id) as total from cates"
    res1 = db1.find(sql1)
    return jsonify({"code":200,'list':res,'total':res1['total']})




#通过分类id获取分类参数
@bp_course.route('/getCateByid')
def getCateByid():
    id = request.args.get('id')
    sql = "select params from cates where id=%d"%(int(id))
    res = db1.find(sql)
    return jsonify({'code':200,'params':res['params']})




#添加展示课程接口
@bp_course.route('/courseM',methods=['POST','GET'])
def courseM():
    data = get_data()
    if data['params']:
        sql = "insert into courses values(0,'%s',%d,'%s','%s','%s',%d,%d)"%(data['title'],int(data['pid']),data['params'],data['pic'],data['descrip'],int(data['teacher_id']),int(data['status']))
    else:
         sql = "insert into courses(title,pid,pic,descrip,teacher_id,status) values('%s',%d,'%s','%s',%d,%d)"%(data['title'],int(data['pid']),data['pic'],data['descrip'],int(data['teacher_id']),int(data['status']))
   
    db1.update(sql)
    return jsonify({"code":200})